package com.itboye.jinpuyiyuan.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.home.OrderDetailActivity;
import com.itboye.jinpuyiyuan.adapter.PaymentRecordAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.OrderBean;
import com.itboye.jinpuyiyuan.bean.OrderList;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 缴费记录
 */
public class PaymentRecordActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;
    private LinearLayout departmentLl,hospitalLL,testLl;
    private Button hospitalBtn,testBtn,departmentBtn;
    private MaterialRefreshLayout refresh;
    ListView payment_list;
    PaymentRecordAdapter adapter;
    private List<OrderBean> list;
    private BranchAddressPresenter presenter;
    private String uid;
    private String order_type="1"; //挂号
    private int page;
    private boolean isLoadMore=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment_record);

        rightImg.setVisibility(View.GONE);
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("缴费记录");
        page=1;
        uid= SPUtils.get(this,null, Const.UID,"")+"";
        presenter=new BranchAddressPresenter(this);
        list=new ArrayList<>();

        adapter=new PaymentRecordAdapter(this,order_type,list,R.layout.item_panment_records);
        payment_list.setAdapter(adapter);

        setRefresh();
        refresh.autoRefresh();

        payment_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               if (order_type.equals("1")){ //挂号
                   Intent intent = new Intent(PaymentRecordActivity.this, OrderDetailActivity.class);
                   intent.putExtra("order_id",list.get(position).getOrder_code());
                   startActivity(intent);
               }

           }
       });

    }

    public void setRefresh(){
        if (order_type.equals("1")||order_type.equals("3")){
            refresh.setLoadMore(true);
            refresh.setMaterialRefreshListener(new MaterialRefreshListener() {
                @Override
                public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
                    page=1;
                    isLoadMore=false;
                    presenter.getOrderList(uid,order_type,"-1",page+"","10");
                }

                @Override
                public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                    page++;
                    isLoadMore=true;
                    presenter.getOrderList(uid,order_type,"-1",page+"","10");

                }
            });
        }else {
            refresh.setLoadMore(false);
            refresh.setMaterialRefreshListener(new MaterialRefreshListener() {
                @Override
                public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
                    page=1;
                    isLoadMore=false;
                    presenter.getOrderList(uid,order_type,"-1",page+"","10");
                }

//                @Override
//                public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
//                    page++;
//                    isLoadMore=true;
//                    presenter.getOrderList(uid,order_type,"-1",page+"","10");
//
//                }
            });
        }
        departmentLl.setEnabled(false);
        departmentBtn.setEnabled(false);
        hospitalLL.setEnabled(false);
        hospitalBtn.setEnabled(false);
        testLl.setEnabled(false);
        testBtn.setEnabled(false);
        showProgressDialog("数据获取中,请稍等...",false);

    }
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {

            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
//            case R.id.allLl://全部
//            case R.id.allBtn:
//                cancelTable();
//                page=1;
//                order_type="";
//                isLoadMore=false;
//                allBtn.setBackgroundResource(R.drawable.login_corner_bluesolid_btn);
//                allBtn.setTextColor(getResources().getColor(R.color.white));
//                presenter.getOrderList(uid,order_type,"-1",page+"","10");
//                break;

            case R.id.departmentLL://门诊
            case R.id.departmentBtn:
                cancelTable();
                page=1;
                order_type="1";
                isLoadMore=false;
                setRefresh();
                departmentBtn.setBackgroundResource(R.drawable.login_corner_bluesolid_btn);
                departmentBtn.setTextColor(getResources().getColor(R.color.white));
                presenter.getOrderList(uid,order_type,"-1",page+"","10");
                break;

//            case R.id.hospitalLL://住院
//            case R.id.hospitalBtn:
//                cancelTable();
//                page=1;
//                order_type="2";
//                isLoadMore=false;
//                setRefresh();
//                hospitalBtn.setBackgroundResource(R.drawable.login_corner_bluesolid_btn);
//                hospitalBtn.setTextColor(getResources().getColor(R.color.white));
//                presenter.getOrderList(uid,order_type,"-1",page+"","10");
//                break;

            case R.id.testLl://体检
            case R.id.testBtn:
                cancelTable();
                page=1;
                order_type="3";
                isLoadMore=false;
                setRefresh();
                testBtn.setBackgroundResource(R.drawable.login_corner_bluesolid_btn);
                testBtn.setTextColor(getResources().getColor(R.color.white));
                presenter.getOrderList(uid,order_type,"-1",page+"","10");
                break;

        }
    }

    public void cancelTable(){
//        allBtn.setBackgroundResource(R.drawable.login_corner_grayedge);
        departmentBtn.setBackgroundResource(R.drawable.login_corner_grayedge);
        hospitalBtn.setBackgroundResource(R.drawable.login_corner_grayedge);
        testBtn.setBackgroundResource(R.drawable.login_corner_grayedge);
//        allBtn.setTextColor(getResources().getColor(R.color.black));
        departmentBtn.setTextColor(getResources().getColor(R.color.black));
        hospitalBtn.setTextColor(getResources().getColor(R.color.black));
        testBtn.setTextColor(getResources().getColor(R.color.black));
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);

        if (entity!=null){
            try{
                closeProgressDialog();
                if (order_type.equals("1")){
                    hospitalLL.setEnabled(true);
                    hospitalBtn.setEnabled(true);
                    testLl.setEnabled(true);
                    testBtn.setEnabled(true);
                }else if (order_type.equals("2")){

                    departmentLl.setEnabled(true);
                    departmentBtn.setEnabled(true);
                    testLl.setEnabled(true);
                    testBtn.setEnabled(true);
                }else {
                    departmentLl.setEnabled(true);
                    departmentBtn.setEnabled(true);
                    hospitalLL.setEnabled(true);
                    hospitalBtn.setEnabled(true);
                }

            }catch (Exception e){

            }
            if (entity.getEventType() == BranchAddressPresenter.By_Order_orderList_success){ //订单列表
                OrderList orderList= (OrderList) entity.getData();
                List<OrderBean> orders = orderList.getList();
                if (isLoadMore){ //是加载更多
                    this.list.addAll(orders);
                    adapter.addData(list,order_type);
                    refresh.finishRefreshLoadMore();
                }else {
                    if (this.list.size()>0){
                        this.list.clear();
                    }
                    this.list.addAll(orders);
                    adapter.addData(list,order_type);
                    refresh.finishRefresh();
                }

            }else  if (entity.getEventType() == BranchAddressPresenter.By_Order_orderList_fail){

                refresh.finishRefresh();
                refresh.finishRefreshLoadMore();

                MAlert.alert(entity.getMsg());

            }
        }
    }


}
